using System.Collections.Generic;
using System.Net.Http.Formatting;
using System.Web.Http;
using TamoCRM.Core.Commons.Utilities;
using TamoCRM.Domain.SourceTypes;
using TamoCRM.Services.SourceTypes;
using TamoCRM.Web.Framework;
using TamoCRM.Web.Mvc.Areas.Admin.Models.SourceTypes;

namespace TamoCRM.Web.Mvc.Areas.Admin.WebApi.SourceTypes
{
    public class SourceTypeController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<SourceTypeInfo> Get()
        {
            return SourceTypeRepository.GetAll();
        }

        // GET api/<controller>
        public SourceTypeListModel Get(int page, int rows)
        {
            int totalRecords;
            var model = new SourceTypeListModel
                            {
                                Rows = SourceTypeRepository.Search(string.Empty, page, rows, out totalRecords),
                                Page = page,
                                Total = (totalRecords/rows) + 1,
                                Records = rows
                            };
            return model;
        }

        // GET api/<controller>/5
        public SourceTypeInfo Get(int id)
        {
            return SourceTypeRepository.GetInfo(id);
        }

        // POST api/<controller>
        [HttpPost]
        public string Edit(FormDataCollection form)
        {
            string retVal = string.Empty;
            string operation = form.Get("oper");
            int id = ConvertHelper.ToInt32(form.Get("SourceTypeId"));
            if (!string.IsNullOrEmpty(operation))
            {
                SourceTypeInfo info;
                switch (operation)
                {
                    case "edit":
                        info = SourceTypeRepository.GetInfo(id);
                        if (info != null)
                        {
						/*
							
							info.SourceTypeId = form.Get("SourceTypeId");
							
							info.Code = form.Get("Code");
							
							info.Name = form.Get("Name");
							
							info.Description = form.Get("Description");
							
							info.CreatedBy = form.Get("CreatedBy");
							
							info.CreatedDate = form.Get("CreatedDate");
							
							info.ChangedBy = form.Get("ChangedBy");
							
							info.ChangedDate = form.Get("ChangedDate");
							                            
                            SourceTypRepository.Update(info);
							*/
                        }
                        break;
                    case "add":
                        info = new SourceTypeInfo();
						/*
                        
						info.SourceTypeId = form.Get("SourceTypeId");
						
						info.Code = form.Get("Code");
						
						info.Name = form.Get("Name");
						
						info.Description = form.Get("Description");
						
						info.CreatedBy = form.Get("CreatedBy");
						
						info.CreatedDate = form.Get("CreatedDate");
						
						info.ChangedBy = form.Get("ChangedBy");
						
						info.ChangedDate = form.Get("ChangedDate");
						                            
						*/
                        SourceTypeRepository.Create(info);
                        break;
                    case "del":
                        SourceTypeRepository.Delete(id);
                        break;
                }
            }
            StoreData.ListSourceType = SourceTypeRepository.GetAll() ?? new List<SourceTypeInfo>();
            return retVal;
        }
    }
}

